package cn.stylefeng.guns.modular.studentmanage.service;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.fileTag.service.FileTagService;
import cn.stylefeng.guns.modular.studentmanage.mapper.StuManageMapper;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserParam;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserResult;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StuManageService extends ServiceImpl<StuManageMapper, LeaveUserPO> {
    @Autowired
    private StuManageMapper stuManageMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private FileTagService fileTagService;


    public List<LeaveUserResult> getStudentList(Page page, LeaveUserParam leaveUserParam) {
        List<LeaveUserPO> resultList = stuManageMapper.list(page, leaveUserParam.getCondition());
        List<LeaveUserResult> leaveUserResults = new ArrayList<>(resultList.size());
        Map<Long, String> adminUserInfo = getAdminUserInfo();
        resultList.forEach(leaveUserPO -> {
            LeaveUserResult leaveUserResult1 = new LeaveUserResult();
            BeanUtils.copyProperties(leaveUserPO, leaveUserResult1);
            FileTagPO byId = fileTagService.getById(leaveUserPO.getClbumId());
            leaveUserResult1.setClbumName(byId.getTagName());
            leaveUserResult1.setStairApproval(adminUserInfo.get(leaveUserPO.getStairApprovalId()));
            leaveUserResult1.setFoxbaseApproval(adminUserInfo.get(leaveUserPO.getFoxbaseApprovalId()));
            leaveUserResult1.setTertiaryApproval(adminUserInfo.get(leaveUserPO.getTertiaryApprovalId()));
            leaveUserResults.add(leaveUserResult1);
        });
        return leaveUserResults;
    }

    public Map<Long, String> getAdminUserInfo() {
        List<User> userList = userService.list();
        return userList.stream().collect(Collectors.toMap(User::getUserId, User::getName));
    }


    public void consummateInfo(LeaveUserPO leaveUserPO) {
        //根据当前用户获取其所属部门Id及其所属部门名称
        Long sonId = LoginContextHolder.getContext().getUser().getDeptId();
        String sonName = LoginContextHolder.getContext().getUser().getDeptName();
        Dept sonDept = deptService.getById(sonId);
        //获取其父级部门及名称
        Long dadId = sonDept.getPid();
        Dept dadDept = deptService.getById(dadId);
        String dadFullName = dadDept.getFullName();
        //获取其爷级部门id级名称
        Long grandpaId = dadDept.getPid();
        Dept grandpaDept = deptService.getById(grandpaId);
        String grandpaName = grandpaDept.getFullName();
        //获取儿子级别的所属管理员==当前管理员
        leaveUserPO.setStairApprovalId(LoginContextHolder.getContext().getUserId());
        //获取父亲对应的级别的所属的管理员
        User dadUser = userService.getOne(new QueryWrapper<User>().eq("dept_id", dadId));
        leaveUserPO.setFoxbaseApprovalId(dadUser.getUserId());
        //获取爷爷对应的级别的所属管理员
        User grandpaIdUser = userService.getOne(new QueryWrapper<User>().eq("dept_id", grandpaId));
        leaveUserPO.setTertiaryApprovalId(grandpaIdUser.getUserId());
    }
}
